﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GrouponDesktop.AbmRol
{
    public partial class ModifRol : Form
    {
        private string rolModif;
        DataGridViewCheckBoxColumn dgv_add = new DataGridViewCheckBoxColumn();

        public ModifRol(string rol)
        {
            InitializeComponent();
            this.rolModif = rol;
            tb_nom.ReadOnly = true;
        }

        private void ModifRol_Load(object sender, EventArgs e)
        {
            BDConnection.FillComboBox(cb_rol, "select desc_rol from CodingSpree.TL_ROLES");
            dgv_func.Columns.Add(dgv_add);
            dgv_add.ReadOnly = false;
            dgv_add.Name = "agregar";
            dgv_func.Columns[0].ReadOnly = true;
            dgv_add.ValueType = typeof(bool);
        }

        private void b_mf_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dgvR in dgv_func.Rows)
            {

                if ((bool)((DataGridViewCheckBoxCell)dgvR.Cells["agregar"]).FormattedValue)
                {
                    BDConnection.execute("EXEC CodingSpree.insertFunxRol '" + dgvR.Cells["Funcionalidad"].Value.ToString() + "', '" + cb_rol.Text + "'");

                }

                else
                {
                    BDConnection.execute("EXEC CodingSpree.deleteFunxRol '" + dgvR.Cells["Funcionalidad"].Value.ToString() + "', '" + cb_rol.Text + "'");
                }
            }

            MessageBox.Show("ROL MODIFICADO");
        }

        private void cb_rol_SelectedIndexChanged(object sender, EventArgs e)
        {

            BDConnection.FillGrid("select desc_funcionalidad as Funcionalidad from CodingSpree.TL_FUNCIONALIDADES", dgv_func);
            dgv_func.Columns[0].Width = 50;
            dgv_func.Columns[1].Width = 400;

            foreach (DataGridViewRow dgvR in dgv_func.Rows)
            {
                int check = BDConnection.QueryAndExecuteScalar("EXEC CodingSpree.checkFunc '" + dgvR.Cells["Funcionalidad"].Value.ToString() + "' , '" + cb_rol.Text + "'");
                if (check == 1)
                {
                    dgvR.Cells["agregar"].Value = true;
                }
            }
            dgv_add.ReadOnly = false;

            tb_nom.ReadOnly = false;
            tb_nom.Text = cb_rol.Text;

        }

        private void b_Alta_Click(object sender, EventArgs e)
        {
            BDConnection.execute("EXEC CodingSpree.altaLogica '" + cb_rol.Text + "'");
            MessageBox.Show("Rol dado de Alta nuevamente");
        }

        private void b_camb_Click(object sender, EventArgs e)
        {
            BDConnection.execute("EXEC CodingSpree.updNomRol '" + cb_rol.Text + "' , '" + tb_nom.Text + "'");
            MessageBox.Show("Nombre Cambiado");
        }



    }
}
